# -*- coding: utf-8 -*-
from protos import greater_pb2, greater_pb2_grpc
import crud
from database import ScopedSession

# Greeter服务的实现
class GreeterRcpServic(greater_pb2_grpc.GreeterServicer):
    def SayHello(self, request, context):
        print(f"Received request: {request.name}")
        name = request.name
        # 根据请求的name返回一个问候语
        if "article_id" in request.name:
            article_id = request.name.split('=')[-1]
            db = ScopedSession()
            article = crud.get_article(db, article_id)
            if article:
                name  = article.title
        return greater_pb2.HelloReply(message=f'Hello, {name}!')